package com.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.system.mapper.AssessmentByOrgMapper;
import com.system.pojo.AssessmentByOrg;
import com.system.service.AssessmentByOrgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service("assessmentByOrgService")
public class AssessmentByOrgServiceImpl implements AssessmentByOrgService {
    @Resource
    AssessmentByOrgMapper assessmentByOrgMapper;
    @Override
    public List<AssessmentByOrg> select(AssessmentByOrg assessmentByOrg) {
        return assessmentByOrgMapper.select(assessmentByOrg);
    }

    @Override
    public int insert(AssessmentByOrg assessmentByOrg) {
        return assessmentByOrgMapper.insert(assessmentByOrg);
    }

    @Override
    public int update(AssessmentByOrg assessmentByOrg) {
        return assessmentByOrgMapper.update(assessmentByOrg);
    }

    @Override
    public int delete(String id) {
        return assessmentByOrgMapper.delete(id);
    }

    @Override
    public String selectOrgAss(String id) {
        AssessmentByOrg ass = new AssessmentByOrg();
        ass.setOrgid(id);
        ass.setIsturnassessment("Y");
        ass.setIsturn("Y");
        List<AssessmentByOrg> list = assessmentByOrgMapper.select(ass);
        JSONArray array = new JSONArray();
        for (int i = 0; i < list.size(); i++) {
            AssessmentByOrg assessmentByOrg = list.get(i);
            JSONObject object = new JSONObject();
            object.put("value", assessmentByOrg.getAssessment());
            object.put("title", assessmentByOrg.getAssessmentName());
            array.add(object);
        }
        return array.toJSONString();
    }

    @Override
    public String saveAssessment(List<AssessmentByOrg> list) {
        assessmentByOrgMapper.updatekh(list.get(0).getOrgid());
        assessmentByOrgMapper.updateqy(list.get(0).getOrgid());
        for (int i = 0; i < list.size(); i++) {
            AssessmentByOrg assessmentByOrg = list.get(i);
            System.out.println(assessmentByOrg.toString());
            String isturn = assessmentByOrg.getIsturn();
            String isturnassessment = assessmentByOrg.getIsturnassessment();
            assessmentByOrg.setIsturn(null);
            assessmentByOrg.setIsturnassessment(null);
            List<AssessmentByOrg> a2 = assessmentByOrgMapper.select(assessmentByOrg);
            assessmentByOrg.setIsturn(isturn);
            assessmentByOrg.setIsturnassessment(isturnassessment);
            if (a2 != null && a2.size() > 0) {
                System.out.println("进行修改，权限ID为"+a2.get(0).getId());
                assessmentByOrg.setId(a2.get(0).getId());
                if(assessmentByOrgMapper.update(assessmentByOrg) > 0){
                    System.out.println("第"+(i+1)+"条修改成功");
                } else {
                    System.out.println("第"+(i+1)+"条修改失败");
                }
            } else {
                System.out.println("进行添加");
                if(assessmentByOrgMapper.insert(assessmentByOrg) > 0){
                    System.out.println("第"+(i+1)+"条添加成功");
                } else {
                    System.out.println("第"+(i+1)+"条添加失败");
                }
            }
            System.out.println("执行进度："+(i+1)+"/"+list.size());
            //System.out.println(assessmentByOrg.toString());
        }
        return "执行成功！";
    }

    public AssessmentByOrgMapper getAssessmentByOrgMapper() {
        return assessmentByOrgMapper;
    }

    public void setAssessmentByOrgMapper(AssessmentByOrgMapper assessmentByOrgMapper) {
        this.assessmentByOrgMapper = assessmentByOrgMapper;
    }
}
